<?php


namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;

class Base extends Controller
{
//    public $expiration_time = time()+1200;
    public function _initialize()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,bz-token");
        header('Access-Control-Allow-Methods: GET, POST');
//        $head = $this->request = Request::instance();
//        $token = '1561902079978047f287266bc89bb1343626165f81';
//        $value = Db::name('admin')->field('id,expiration_time')->where(['token'=>$token])->find();
//        halt($value);
//        if($value){
//            if(time() > $value['expiration_time']){
//                $this->apiReturn(303,'token已经过期，请从新登录');
//            }else{
//                Db::name('admin')->update(['id'=>$value['id'],'expiration_time'=>config('expiration_time')]);
//            }
//        }else{
//            $this->apiReturn(303,'token值不存在，非法获取');
//        }
    }

    /**
     * api接口返回友好信息
     * @param number $code   状态
     * @param string $msg    状态描述
     * @param unknown $data  返回数据
     */
    public function apiReturn( $code=200 , $msg='', $data = array() ,$total=0 ){
        $returnData = array(
            'code' => $code,
            'msg'  => $msg?$msg:return_msg($code),
            'data' => $data,
            'total'=>$total
        );
        if(I('callback')){
            echo I('callback').'('.json_encode($returnData).')';die;
        }
        echo json_encode($returnData);die;
    }
}